package com.teach;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * <pre>
 * - 简单的静态http服务器
 * Author : J.L.Zhou
 * E-Mail : 2233875735@qq.com
 * Tel : 151 1104 7708
 * Date : 2021-5-6 10:27:05
 * Version : 1.0
 * Copyright 2021 jlzhou.top Inc. All rights reserved.
 * Warning: this content is only for internal circulation of the company.
 *          It is forbidden to divulge it or use it for other commercial purposes.
 * </pre>
 */
public class HttpServer {

	public static void main(String[] args)throws Exception {
		ServerSocket server = new ServerSocket(88);
		System.out.println("创建了http服务器:88");
		while(true) {
			Socket client = server.accept();
			InputStream in = client.getInputStream();
			BufferedReader request = new BufferedReader(new InputStreamReader(in,"UTF-8"));
			String line = null;
			while(!"".equals((line=request.readLine()))) {
				System.out.println(line);
			}
			System.out.println("读取请求信息完成");
			
			OutputStream out = client.getOutputStream();
			PrintWriter response = new PrintWriter(new OutputStreamWriter(out, "UTF-8"));
			response.println("HTTP/1.1 200 ok");
			response.println("Content-Type:text/html;charset=UTF-8");
			response.println();//空行后面是响应正文
			response.println("<h1>你好!这是一个简单的静态http服务器</h1>");
			response.flush();
			System.out.println("发送响应完成");
			
		}
		
	}
}
